window.onload = function(){

    var main = document.querySelector('.main');
    var sport = document.querySelector('.sport');
    var join = document.querySelector('.join');

    // 此属性为真，则可以领取证书；为假，则不能领取
    var certificate = true;

    var lotteryLeft = document.querySelector('.lottery-left');
    var lotteryRight = document.querySelector('.lottery-right');

    var cover = document.querySelector('.cover');
    var bomb = document.querySelector('.bomb');
    var bomb2 = document.querySelector('.bomb2');
    var topText = document.querySelector('.top-text');
    var tipTop = document.querySelector('.tip-top');
    var warn = document.querySelector('.warn');
    var warnText = document.querySelector('.warn-text');
    var warnTitle = document.querySelector('.warn-title');
    var warnLittle = document.querySelector('.warn-little');
    var warnBeggin = document.querySelector('.warn-beggin');
    var warnSubbg = document.querySelector('.warn-subbg');
    var warnContinue = document.querySelector('.warn-continue');
    var warnNumber = document.querySelector('.warn-number');
    var not = document.querySelector('.not');
    var end = document.querySelector('.end');

    var win = document.querySelector('.win');
    var submitbg = document.querySelector('.submitbg');
    var submit = document.querySelector('.submit');

    var rate = (375 / 603).toFixed(2);
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    document.body.style.width = Math.floor(rate * height) + 'px';

    // 剩余抽奖次数
    /*
    * 当 lotteryTimes = 0 时，表示抽奖机会已经全部用完
    */
    var lotteryTimes = 0;
    // var times = function(num){
    //     lotteryTimes = num;
    // };
    // times(101);

    var hide = function(){
        win.className = 'win none';
        end.className = 'end none';
        warn.className = 'warn none';
        warnBeggin.className = 'warn-beggin none';
        topText.className = 'top-text none';
        warnTitle.className = 'warn-title none';
        not.className = 'not none';
        tipTop.className = 'tip-top block';
        warnLittle.className = 'warn-little block';
        warnText.className = 'warn-text block';
        warnSubbg.className = 'warn-subbg block';
        warnContinue.className = 'warn-continue block';
        bomb.className = 'bomb to-bottom height32';
    };

    function temp(){
        win.className = 'win none';
        warnLittle.className = 'warn-title none';
        tipTop.className = 'tip-top none';
        warnSubbg.className = 'warn-subbg none';
        warnContinue.className = 'warn-continue none';
        not.className = 'not none';
        warnBeggin.className = 'warn-beggin none';
        warnText.className = 'warn-text none';
        bomb.className = 'bomb to-bottom';
    }

    cover.onclick = function(){
        cover.className = 'cover none';
    };

    warnSubbg.onclick = function(event){
        cover.className = 'cover none';
        setTimeout(function(){
            lotteryRight.click();
        }, 10);

        event.stopPropagation();
    };
    warnContinue.onclick = function(event){
        cover.className = 'cover none';
        setTimeout(function(){
            lotteryRight.click();
        }, 10);
        event.stopPropagation();
    };

    // 点击 抽奖
    lotteryRight.onclick = function(){

        var domain = 'http://irun-dev.hupu.com/';
        $.ajax({
            method: 'GET',
            url: domain + 'index.php?c=show&a=Draw',
            data: {}
        }).done(function(data){
            var index = data.indexOf('<');
            console.log(data.substr(0, index));
            data = JSON.parse(data.substr(0, index));
            console.log(data.result);
            lotteryTimes = data.result.id;
        }).fail(function(error){
            console.log(error);
        });

        // lotteryTimes--;
        cover.className = 'cover show';

        switch (lotteryTimes){
            case 0:
                temp();
                warn.className = 'warn none';
                end.className = 'end block';
                break;
            case 1:
                hide();
                warnNumber.innerText = '1';
                warn.className = 'warn none';
                break;
            case 2:
                hide();
                warnNumber.innerText = '2';
                break;
            case 3:
                hide();
                warnNumber.innerText = '3';
                break;
            case 4:
                hide();
                warnNumber.innerText = '4';
                break;
            case 5:
                hide();
                warnNumber.innerText = '5';
                break;
            case 6:
                hide();
                warnNumber.innerText = '6';
                break;
            case 7:
                hide();
                warnNumber.innerText = '7';
                break;
            case 100: // 中奖
                hide();
                win.className = 'win block';
                warnText.className = 'warn-text none';
                warnContinue.className = 'warn-continue none';
                warnSubbg.className = 'warn-subbg none';
                warnLittle.className = 'warn-little none';
                bomb.className = 'bomb2 to-bottom height68';
                tipTop.style.top = '-7%';
                break;
            default:
                temp();
                warn.className = 'warn block';
                end.className = 'end none';
                break;
        }
    };

    bomb.onclick = function(event){

        event.stopPropagation();
    };

    // 点击 个人证书
    lotteryLeft.onclick = function(){
        if(certificate){
            window.location = 'certificate';
        }else{
            win.className = 'win none';
            not.className = 'not block';
            end.className = 'end none';
            topText.className = 'top-text block';
            warnTitle.className = 'warn-title block';
            cover.className = 'cover show';
            warnLittle.className = 'warn-title none';
            tipTop.className = 'tip-top none';
            warn.className = 'warn none';
            warnSubbg.className = 'warn-subbg none';
            warnContinue.className = 'warn-continue none';
            warnText.className = 'warn-text none';
            warnBeggin.className = 'warn-beggin none';
            bomb.className = 'bomb to-bottom';
        }

    };

};
